package com.ww.basic.oop

import scala.collection.mutable

class Marker2 private (val color: String) {
  println(s"Creating ${this}")

  override def toString = s"marker color $color"
}

/**
 * 伴生对象
 * 伴生对象和相应的伴生类可以放同一个文件中
 */
object Marker2 {
  private val markers = mutable.Map(
    "red" -> new Marker2("red"),
    "blue" -> new Marker2("blue"),
    "yellow" -> new Marker2("yellow")
  )

  def getMarker(color: String): Marker2 =
    markers.getOrElseUpdate(color, new Marker2(color))
}

object TestMarker2 {
  def main(args: Array[String]): Unit = {
    println(Marker2.getMarker("blue"))
    println(Marker2.getMarker("blue"))
    println(Marker2.getMarker("red"))
    println(Marker2.getMarker("red"))
    println(Marker2.getMarker("green"))
  }
}